package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.enums.eUnionSyncType;
import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.entity.union.cross.CrossUnionMember;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.union.CrossUnionMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = Protocol.C_UNION_CONSTRUCT_ACTIVE_REWARD, desc = "领取建设活跃度奖励")
public class GetConstructActiveRewardCmd extends CrossCommand {

    long playerId;
    String reward = "";
    String unionUid = "";
    List<Integer> activeIndex = new ArrayList<>();
    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {

        UnionProto.UnionConstructActiveRewardReqMsg reqMsg = UnionProto.UnionConstructActiveRewardReqMsg.parseFrom(packet.getBytes());

        int reqConstructActive = reqMsg.getConstructActive();

        CrossUserBaseInfo crossUserBaseInfo = CrossUserMgr.getCrossUserBaseInfo(packet.getPlayerId());
        if(crossUserBaseInfo == null){
            ret = GameErrorCode.E_USER_NO_FOUND;
            return;
        }

        CrossUnionMember member = CrossUnionMgr.getUnionMember(crossUserBaseInfo.getUserBaseInfo().getUnionUid(), playerId);
        if(member == null || StringUtils.isNullOrEmpty(member.getUnionUid())){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        CrossUnionInfo unionInfo = CrossUnionMgr.getUnionInfo(member.getUnionUid());
        if(unionInfo == null){
            ret = GameErrorCode.E_UNION_NO_JOIN_UNION;
            return;
        }

        unionUid = unionInfo.getUnionUid();

        boolean hasReward = false;
        Property rewardProperty = new Property();
        for(Integer constructActive : ConfigMgr.getUnionConstructActiveList()){
            int index = ConfigMgr.getUnionConstructActiveIndex(constructActive);
            if (-1 == index) {
                continue;
            }
            if(crossUserBaseInfo.getUserBaseInfo().getTitleId() < GameConfig.TASK_DAILY_ACTIVE_GET_ALL_REWARD_NEED_LV){
                //身份限制，单个单个领取
                if(reqConstructActive != constructActive){
                    continue;
                }
            }
            if(CrossUnionMgr.isCanRewardConstruct(unionInfo, member, constructActive) != 0){
                continue;
            }
            member.addConstructRewardIndex(constructActive);
            activeIndex.add(index);
            rewardProperty.addProperty(ConfigMgr.getUnionConstructActiveReward(index));
            hasReward = true;
        }

        if(!hasReward){
            ret = GameErrorCode.E_UNION_CONSTRUCT_ACTIVE_NO_ENOUGH;
        }
        reward = PropertyHelper.parsePropertyToString(rewardProperty);
    }

    @Override
    public void sendData() {

        CrossUnionProto.UnionConstructActiveRewardCrossRespMsg.Builder respMsg = CrossUnionProto.UnionConstructActiveRewardCrossRespMsg.newBuilder();
        respMsg.setRet(ret);
        if(ret == 0){
            respMsg.setReward(reward);
            respMsg.addAllActiveIndex(activeIndex);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_DETAIL, CrossUnionMgr.getUnionDetailMsg(unionUid, eUnionSyncType.Normal.getValue())));
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_UNION_CONSTRUCT_ACTIVE_REWARD_FROM_CROSS, respMsg));
        }else {
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.U_UNION_CONSTRUCT_ACTIVE_REWARD, respMsg));
        }
    }
}
